﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Web;

namespace ZBZH_Manage_Order.PublicMethod
{
    public class AppDictionary
    {
        #region 字典
        /// <summary>
        /// 全局应用程序字典 <Basic_Dict_T.DictKey, Basic_Dict_T>
        /// </summary>
        protected static
            Dictionary<string, Basic_Dict_T> DICS;
        /// <summary>
        /// 字典集合
        /// </summary>
        protected static
            List<Basic_Dict_T> DICLIST;

        public AppDictionary()
        {
            if (DICS == null || DICLIST == null)
            {
                // 构建字典对象
                DICS = new Dictionary<string, Basic_Dict_T>();
                // 构建字典列表对象
                DICLIST = new List<Basic_Dict_T>();
                // 请求服务器数据
                
                string jsonResult = new ZHHQSERVICE.Service1Client().GetBasicDict(0, GetYZM._YZM, 1);
                JsonToObject(jsonResult);   // 解析数据
            }

            new AppCountry();   // 加载国家表

        }
        // 解析数据
        private static void JsonToObject(string jsonResult)
        {
            if (!string.IsNullOrEmpty(jsonResult) && jsonResult.Length > 30)
            {
                // 去掉多余的字符
                jsonResult = jsonResult.Replace("{\"BS\":\"1\",\"Msg\":\"查询成功\",\"total\":0,\"rows\":", "");
                jsonResult = jsonResult.Substring(0, jsonResult.Length - 1);
                // 获取Json序列化对象
                DataContractJsonSerializer json = new DataContractJsonSerializer(DICLIST.GetType());
                // 将Json字符串转成字节
                byte[] operUsersByte = System.Text.Encoding.UTF8.GetBytes(jsonResult);
                // 将字节数组装入内存
                System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(operUsersByte);
                // 设置开始位置
                memoryStream.Position = 0;
                // 将数据转化为要的对象(填充列表)
                DICLIST = (List<Basic_Dict_T>)json.ReadObject(memoryStream);
                // 填充字典
                foreach (Basic_Dict_T dic in DICLIST)
                {
                    DICS.Add(dic.DictKey, dic);   // 保存进入字典
                }
            }
        }

        /// <summary>
        /// 根据 DictKey 获取字典对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns>对象</returns>
        public static Basic_Dict_T GetDicByDictKey(string id)
        {
            return DICS[id];
        }

        /// <summary>
        /// 获取数据字典集合
        /// </summary>
        /// <returns>字典集合</returns>
        public static List<Basic_Dict_T> GetDicList()
        {
            return DICLIST;
        }
        #endregion

        /*******************************新增方法*********************************/

        #region 国家
        /// <summary>
        /// 获取国家表数据
        /// </summary>
        public class AppCountry
        {
            protected static
                Dictionary<string, ZBZH_Manage_Order.Models.Basic_Country_T> COUS;
            protected static
                List<ZBZH_Manage_Order.Models.Basic_Country_T> COULIST;

            public AppCountry()
            {
                if (COUS == null || COULIST == null)
                {
                    COUS = new Dictionary<string, ZBZH_Manage_Order.Models.Basic_Country_T>();
                    COULIST = new List<ZBZH_Manage_Order.Models.Basic_Country_T>();
                    // 获取国家
                    string jsonList_Country = new ZHHQSERVICE.Service1Client().GetCountryList(GetYZM._YZM, 1);
                    JsonToObject(jsonList_Country);
                }


                new AppRegion();    // 加载地区表
            }
            // 解析数据
            private static void JsonToObject(string jsonResult)
            {
                if (!string.IsNullOrEmpty(jsonResult) && jsonResult.Length > 30)
                {
                    // 去掉多余的字符
                    jsonResult = jsonResult.Replace("{\"BS\":\"1\",\"Msg\":\"查询成功\",\"total\":0,\"rows\":", "");
                    jsonResult = jsonResult.Substring(0, jsonResult.Length - 1);
                    // 获取Json序列化对象
                    DataContractJsonSerializer json = new DataContractJsonSerializer(COULIST.GetType());
                    // 将Json字符串转成字节
                    byte[] operUsersByte = System.Text.Encoding.UTF8.GetBytes(jsonResult);
                    // 将字节数组装入内存
                    System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(operUsersByte);
                    // 设置开始位置
                    memoryStream.Position = 0;
                    // 将数据转化为要的对象(填充列表)
                    COULIST = (List<ZBZH_Manage_Order.Models.Basic_Country_T>)json.ReadObject(memoryStream);
                    // 填充字典
                    foreach (ZBZH_Manage_Order.Models.Basic_Country_T dic in COULIST)
                    {
                        COUS.Remove(dic.CountryID);
                        COUS.Add(dic.CountryID, dic);   // 保存进入字典
                    }
                }
            }

            /// <summary>
            /// 根据 DictKey 获取字典对象
            /// </summary>
            /// <param name="id"></param>
            /// <returns>对象</returns>
            public static ZBZH_Manage_Order.Models.Basic_Country_T GetDicByDictKey(string id)
            {
                return COUS[id];
            }

            /// <summary>
            /// 获取数据字典集合
            /// </summary>
            /// <returns>字典集合</returns>
            public static List<ZBZH_Manage_Order.Models.Basic_Country_T> GetCouList()
            {
                return COULIST;
            }

        }

        #endregion

        #region 地区
        /// <summary>
        /// 获取地区表数据
        /// </summary>
        public class AppRegion
        {
            protected static
                Dictionary<string,  ZBZH_Manage_Order.Models.Basic_Region_T> REGS;
            protected static
                List<ZBZH_Manage_Order.Models.Basic_Region_T> REGLIST;

            public AppRegion()
            {
                if (REGS == null || REGLIST == null)
                {
                    REGS = new Dictionary<string, ZBZH_Manage_Order.Models.Basic_Region_T>();
                    REGLIST = new List<ZBZH_Manage_Order.Models.Basic_Region_T>();
                    // 获取国家
                    string jsonList_Region = new ZHHQSERVICE.Service1Client().GetRegionList("", GetYZM._YZM, 1);
                    JsonToObject(jsonList_Region);
                }
            }
            // 解析数据
            private static void JsonToObject(string jsonResult)
            {
                if (!string.IsNullOrEmpty(jsonResult) && jsonResult.Length > 30)
                {
                    // 去掉多余的字符
                    jsonResult = jsonResult.Replace("{\"BS\":\"1\",\"Msg\":\"查询成功\",\"total\":0,\"rows\":", "");
                    jsonResult = jsonResult.Substring(0, jsonResult.Length - 1);
                    // 获取Json序列化对象
                    DataContractJsonSerializer json = new DataContractJsonSerializer(REGLIST.GetType());
                    // 将Json字符串转成字节
                    byte[] operUsersByte = System.Text.Encoding.UTF8.GetBytes(jsonResult);
                    // 将字节数组装入内存
                    System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(operUsersByte);
                    // 设置开始位置
                    memoryStream.Position = 0;
                    // 将数据转化为要的对象(填充列表)
                    REGLIST = (List<ZBZH_Manage_Order.Models.Basic_Region_T>)json.ReadObject(memoryStream);
                    // 填充字典
                    foreach (ZBZH_Manage_Order.Models.Basic_Region_T dic in REGLIST)
                    {
                        REGS.Remove(dic.RegionID);
                        REGS.Add(dic.RegionID, dic);   // 保存进入字典
                    }
                }
            }

            /// <summary>
            /// 根据 DictKey 获取字典对象
            /// </summary>
            /// <param name="id"></param>
            /// <returns>对象</returns>
            public static ZBZH_Manage_Order.Models.Basic_Region_T GetDicByDictKey(string id)
            {
                return REGS[id];
            }

            /// <summary>
            /// 获取数据字典集合
            /// </summary>
            /// <returns>字典集合</returns>
            public static List<ZBZH_Manage_Order.Models.Basic_Region_T> GetRegList()
            {
                return REGLIST;
            }
        }
        #endregion
    }
}